package com.woniu.supermarket.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniu.supermarket.entity.PurchaseReturn;
import com.woniu.supermarket.entity.PurchaseReturnCheck;
import com.woniu.supermarket.entity.PurchaseReturnDetail;
import com.woniu.supermarket.mapper.PurchaseReturnCheckMapper;
import com.woniu.supermarket.mapper.PurchaseReturnDetailMapper;
import com.woniu.supermarket.mapper.PurchaseReturnMapper;
import com.woniu.supermarket.service.IPurchaseReturnService;
import com.woniu.supermarket.util.CodeCreateUtil;
import com.woniu.supermarket.vo.PurchaseReturnDetailVo;
import com.woniu.supermarket.vo.PurchaseReturnVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service
public class PurchaseReturnService implements IPurchaseReturnService {

    @Autowired
    PurchaseReturnMapper purchaseReturnMapper;

    @Autowired
    PurchaseReturnDetailMapper purchaseReturnDetailMapper;

    @Autowired
    PurchaseReturnCheckMapper purchaseReturnCheckMapper;

    @Override
    public PageInfo<PurchaseReturnVo> findByCons(Integer now, Integer size, String returnNumber,Integer status) {
        PageHelper.startPage(now,size);
        List<PurchaseReturnVo> lists = purchaseReturnMapper.selectByCons(returnNumber,status);
        return new PageInfo<>(lists);
    }

    @Override
    public List<PurchaseReturn> findAll() {
        return purchaseReturnMapper.selectAll();
    }

    @Override
    @Transactional
    public int modifyPurchaseReturn(PurchaseReturnVo purchaseReturnVo) {
        //1.先将该订单下的订单详情删除
        purchaseReturnDetailMapper.deleteByPurchaseReturnId(purchaseReturnVo.getPurchaseId());
        //2.修改订单信息
        purchaseReturnMapper.updateByPurchaseReturnVo(purchaseReturnVo);

        //添加采购退货单详情
        List<PurchaseReturnDetailVo> purchaseReturnDetailList = purchaseReturnVo.getPurchaseReturnDetailList();
        for(PurchaseReturnDetailVo p : purchaseReturnDetailList){
            PurchaseReturn pu = new PurchaseReturn();
            pu.setId(purchaseReturnVo.getId());
            p.setPurchaseReturn(pu);
            p.setGoodsId(p.getGoods().getId());
            p.setReturnDetailNumber(CodeCreateUtil.createPurchaseReturnDetailCode());
        }

        int i = purchaseReturnDetailMapper.insertBatchPurchaseReturnDetail(purchaseReturnDetailList);

        return i;
    }

    @Override
    @Transactional
    public int removePurchaseReturnById(Integer id) {
        int i = purchaseReturnMapper.deleteByPrimaryKey(id);
        purchaseReturnDetailMapper.deleteByPurchaseReturnId(id);
        return i;
    }

    @Override
    @Transactional
    public int addPurchaseReturn(PurchaseReturnVo purchaseReturnVo) {
        //添加采购退货单
        purchaseReturnVo.setReturnNumber(CodeCreateUtil.createPurchaseReturnCode());
        purchaseReturnMapper.insert(purchaseReturnVo);
        //System.out.println(purchaseReturnVo.getId()+"===id");
        //添加采购退货单详情
        List<PurchaseReturnDetailVo> purchaseReturnDetailList = purchaseReturnVo.getPurchaseReturnDetailList();
        for(PurchaseReturnDetailVo p : purchaseReturnDetailList){
            PurchaseReturn pu = new PurchaseReturn();
            pu.setId(purchaseReturnVo.getId());
            p.setPurchaseReturn(pu);
            p.setReturnDetailNumber(CodeCreateUtil.createPurchaseReturnDetailCode());
        }

        int i = purchaseReturnDetailMapper.insertBatchPurchaseReturnDetail(purchaseReturnDetailList);

        return i;
    }

    @Override
    @Transactional
    public int auditPurchaseReturnCheck(PurchaseReturnCheck purchaseReturnCheck) {
        //1.先修改采购退货订单状态
        int status = purchaseReturnCheck.getCheckResult();
        String code = purchaseReturnCheck.getReturnNumber();
        purchaseReturnMapper.updateByCode(code,status);
        System.out.println(status+"====状态");
        System.out.println(code+"====编码");


        //2.将审核信息插入到审核信息表
        int i = purchaseReturnCheckMapper.insert(purchaseReturnCheck);
        return i;
    }

    @Override
    public PageInfo<PurchaseReturnVo> findByCons1(Integer now, Integer size, String returnNumber, Integer status, Integer purchaseId) {
        PageHelper.startPage(now,size);
        List<PurchaseReturnVo> lists = purchaseReturnMapper.selectByCons1(returnNumber,status,purchaseId);
        return new PageInfo<>(lists);
    }


}
